



*---------------------------------folders-------------------
if "`c(username)'"=="da334" | "`c(username)'"=="David" | "`c(username)'"=="Atkin" | "`c(username)'"=="dga" | "`c(username)'"=="atkin" {
global dump "C:/Scratch"
global dropbox "C:/Work/Engel_GFT"
global stataloc "C:/Dropbox/Stata15/StataMP-64"
global codeloc "$dropbox/replication_files/do_files"
global output "$dropbox/replication_files/data/intermediate_data/conventional_price_indices"
}
if "`c(username)'"=="dga" {
global dump "G:/Scratch" 
}
*-------------------------------------------------------------------




clear all
set more off
set matsize 11000







foreach ender in V1_D   {  

use "$dropbox/replication_files/data/intermediate_data/R43R55/expenditures_R43R55_i_groups`ender'.dta", clear

gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector

drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.



cap drop sum_exp_supergroup 
cap drop budget_share



*now household file
*preserve

gegen tag=tag(hhid)
keep if tag==1
drop tag
*% get unique market-round identifier
egen market_year_id=group(state43 district43 sector round)
egen market_id=group(state43 district43 sector)
gen logexp_cap=ln(mpce)
****Winsorize 1 Percent on Either Side of LogExpCap Distribution***
    *XXX Winsorize at the household level, right now it is using household X product level observations. Use tag() to do this.
*winsor2 logexp_cap, suffix(w) cuts(1 99) by(market_year_id)
winsor2 logexp_cap, suffix(w) cuts(0.1 99.9) by(round sector)

*Integer weights for lpoly:
gen int_wt=int(wt)

*count housheolds in cell
egen count_hh=count(hhid),by(market_year_id)

egen total_wt=total(wt),by(market_year_id)

keep round sector hhid wt state43 district43 market_year_id market_id logexp_cap logexp_capw int_wt count_hh total_wt hhsize flag* 




keep if sector==1
cap drop if flag_mpce==1 // these have extreme mpces


}




qui {

keep if logexp_capw!=.

rename logexp_capw lmpce

sum market_id
local markets=r(max)



foreach round in 43  55 { 
foreach var of varlist lmpce  {

gen r`round'_`var'_n=.

}
}


egen tag=tag(market_id)



forval mkt=1/`markets' {
noi di "`mkt'"
foreach round in 43 55 {




foreach var of varlist lmpce  {

sum `var' if round==`round' & market_id==`mkt' , d

replace r`round'_`var'_n=r(sum_w) if  market_id==`mkt' 
 



}



}

drop if tag==0 & market_id==`mkt'



}





keep sector market_id district43 state43 r43* r55*

}

save "$dropbox/replication_files/data/intermediate_data/R43R55/market_characteristics_43_55_rural.dta", replace

